//
//  MLISThreeView.m
//  BaseProject
//
//  Created by mac on 2021/1/11.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "MLISThreeView.h"
#import "TT_WebviewController.h"
@interface MLISThreeItemView : UIView
@property(nonatomic,weak)BaseVC *parentVC;
@property(nonatomic,assign)NSInteger index;
@property(nonatomic,strong)UIImageView *leftIV;
@property(nonatomic,strong)UILabel *titleLab;
@property(nonatomic,strong)UIButton *rightButton;
@property(nonatomic,assign)BOOL isClose;
@property(nonatomic,assign)CGFloat flexHeight;
@property(nonatomic,assign)CGFloat closeHeight;
@property(nonatomic,copy)void (^callBackChangeHeightBlock)(void);
@end
@implementation MLISThreeItemView
- (void)setIndex:(NSInteger)index
{
    _index = index;
    self.clipsToBounds = YES;
    [self leftIV];
    [self rightButton];
    if (index == 0) {
        self.titleLab.text = @"什么是“绝对免赔额”?";
        YYLabel *lab = [[YYLabel alloc]init];
        [self addSubview:lab];
        lab.frame = CGRectMake(48*ADAPTER_WIDTH, 15*ADAPTER_WIDTH + self.titleLab.bottom, self.width - 67*ADAPTER_WIDTH, 0);
        lab.numberOfLines = 0;
        
        NSMutableAttributedString *titleAttributes = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",@"绝对免赔额是保险合同中规定的保险人对约定数额以下的损失绝对不承担赔偿责任的免赔限额。在保险标的发生损失时，必须超过一定金额或比率，保险人才对超过部分承担赔偿责任，损失在规定限额以下的，保险人不予赔偿。"]];
        titleAttributes.font = kFontRegular14;
        titleAttributes.color = kColor102;
        titleAttributes.lineSpacing = 5*ADAPTER_WIDTH;
        
        lab.attributedText = titleAttributes;
        lab.height = [YYTextLayout layoutWithContainerSize:CGSizeMake(lab.width, MAXFLOAT) text:titleAttributes].textBoundingSize.height;
        self.height = lab.bottom;
    } else {
        self.titleLab.text = @"是否所有情况都可以获得赔付?";
        
        YYLabel *lab = [[YYLabel alloc]init];
        [self addSubview:lab];
        lab.frame = CGRectMake(48*ADAPTER_WIDTH, 15*ADAPTER_WIDTH + self.titleLab.bottom, self.width - 67*ADAPTER_WIDTH, 0);
        lab.numberOfLines = 0;
        
        NSMutableAttributedString *titleAttributes = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",@"被保险人因下列原因而导致身故或残疾 的，保险人不承担给付保险金责任："]];
        titleAttributes.font = kFontRegular14;
        titleAttributes.color = kColor51;
        titleAttributes.lineSpacing = 5*ADAPTER_WIDTH;
        
        lab.attributedText = titleAttributes;
        lab.height = [YYTextLayout layoutWithContainerSize:CGSizeMake(lab.width, MAXFLOAT) text:titleAttributes].textBoundingSize.height;
        
        CGFloat orinY = 8*ADAPTER_WIDTH + lab.bottom;
        NSArray<NSString *> *dataSoruce = @[@"投保人的故意行为；",@"故意自伤或自杀，但被保险人自杀时为无民事行为能力人的除外；",@"因被保险人挑衅或故意行为而导致的打斗、被袭击或被谋杀；",@"妊娠、流产、分娩、药物过敏；",@"接受包括美容、整容、整形手术在内的任何医疗行为而造成的意外；",@"未遵医嘱服用、涂用、注射药物；",@"受酒精、毒 品、管制药物的影响；",@"疾病，包括但不限于高原反应、中暑、猝死（释义见 8.4 ）；",@"非因意外伤害导致的细菌或病毒感染；",@"任何生物、化学、原子能武器，原子能或核能装置所造成的爆炸、灼伤、污染或辐射；",@"恐怖袭击；",@"详情查看《公众责任保险条款（1999版）》《意外伤害保险条款》"];
        for (NSInteger index = 0; index < dataSoruce.count; index ++) {
            UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(49*ADAPTER_WIDTH, orinY + 7*ADAPTER_WIDTH, 4*ADAPTER_WIDTH, 5*ADAPTER_WIDTH)];
            [self addSubview:iv];
            iv.image = [UIImage getPNGimageInBundleWithName:@"MLI_Segement_3_1"];
            
            YYLabel *itemLab = [[YYLabel alloc]init];
            [self addSubview:itemLab];
            itemLab.frame = CGRectMake(57*ADAPTER_WIDTH, orinY, self.width - 67*ADAPTER_WIDTH, 0);
            itemLab.numberOfLines = 0;
            
            NSMutableAttributedString *itemTitleAttributes = [[NSMutableAttributedString alloc]initWithString:dataSoruce[index]];
            itemTitleAttributes.font = kFontRegular14;
            itemTitleAttributes.color = kColor102;
            itemTitleAttributes.lineSpacing = 5*ADAPTER_WIDTH;
            
            if (index == dataSoruce.count - 1) {
                itemTitleAttributes.color = rgba(255, 85, 57, 1);
                [itemTitleAttributes setColor:kColor102 range:NSMakeRange(0, 4)];
                WK(weakSelf)
                [itemTitleAttributes setTextHighlightRange:NSMakeRange(4, 17) color:rgba(255, 85, 57, 1) backgroundColor:[UIColor lightGrayColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                    TT_WebviewController *vc = [[TT_WebviewController alloc]init];
                    vc.webUrl = [NSString stringWithFormat:@"%@/%@", BA_GlobalPdf_Domain, kInsure2Pdf];
                    [weakSelf.parentVC.navigationController pushViewController:vc animated:YES];
                }];
                [itemTitleAttributes setTextHighlightRange:NSMakeRange(21, 10) color:rgba(255, 85, 57, 1) backgroundColor:[UIColor lightGrayColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                    TT_WebviewController *vc = [[TT_WebviewController alloc]init];
                    vc.webUrl = [NSString stringWithFormat:@"%@/%@", BA_GlobalPdf_Domain, kInsurePdf];
                    [weakSelf.parentVC.navigationController pushViewController:vc animated:YES];
                }];
            }
            
            itemLab.attributedText = itemTitleAttributes;
            itemLab.height = [YYTextLayout layoutWithContainerSize:CGSizeMake(lab.width, MAXFLOAT) text:itemTitleAttributes].textBoundingSize.height;
            
            orinY = itemLab.bottom + 8*ADAPTER_WIDTH;
        }
        
        
        self.height = orinY - 8*ADAPTER_WIDTH;
    }
    
    self.flexHeight = self.height;
    self.closeHeight = self.titleLab.bottom + 15*ADAPTER_WIDTH;
}
#pragma mark - click
- (void)clickRightButton:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.height = self.closeHeight;
    } else {
        self.height = self.flexHeight;
    }
    !self.callBackChangeHeightBlock ?: self.callBackChangeHeightBlock ();
}
#pragma mark - lazy懒加载
- (UIImageView *)leftIV
{
    if (!_leftIV) {
        UIImageView *iv = [[UIImageView alloc]init];
        [self addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"MLI_Segement_3_0"];
        iv.frame = CGRectMake(15*ADAPTER_WIDTH, 19*ADAPTER_WIDTH, 25*ADAPTER_WIDTH, 24*ADAPTER_WIDTH);
        _leftIV = iv;
    }
    return _leftIV;
}
- (UILabel *)titleLab
{
    if (!_titleLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightMedium];
        lab.textColor = kColor51;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(48*ADAPTER_WIDTH, 19*ADAPTER_WIDTH, self.width - 85*ADAPTER_WIDTH, 22*ADAPTER_WIDTH);
        _titleLab = lab;
    }
    return _titleLab;
}
- (UIButton *)rightButton
{
    if (!_rightButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        [button setImage:[UIImage getPNGimageInBundleWithName:@"MLI_Segement_3_up"] forState:UIControlStateNormal];
        [button setImage:[UIImage getPNGimageInBundleWithName:@"MLI_Segement_3_down"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(self.width - 50*ADAPTER_WIDTH, 0, 20*ADAPTER_WIDTH + 30*ADAPTER_WIDTH, 11*ADAPTER_WIDTH + 30*ADAPTER_WIDTH);
        button.imageEdgeInsets = UIEdgeInsetsMake(15*ADAPTER_WIDTH, 14*ADAPTER_WIDTH, 15*ADAPTER_WIDTH, 16*ADAPTER_WIDTH);
        button.centerY = self.titleLab.centerY;
        _rightButton = button;
    }
    return _rightButton;
}
@end

@interface MLISThreeView ()
@property(nonatomic,strong)UIImageView *topIV;
@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)NSMutableArray<MLISThreeItemView *> *itemsArray;
@end

@implementation MLISThreeView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)createProperty
{
    
}
- (void)createUI
{
    WK(weakSelf)
    [self.itemsArray enumerateObjectsUsingBlock:^(MLISThreeItemView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.itemsArray removeAllObjects];
    
    [self bgView];
    [self topIV];
    
    CGFloat orinY = 39*ADAPTER_WIDTH;
    for (NSInteger index = 0; index < 2; index ++) {
        MLISThreeItemView *view = [[MLISThreeItemView alloc]initWithFrame:CGRectMake(0, orinY, self.bgView.width, 0)];
        [self.bgView addSubview:view];
        view.index = index;
        orinY = view.bottom;
        [self.itemsArray addObject:view];
        
        [view setCallBackChangeHeightBlock:^{
            [weakSelf refreshUI];
        }];
    }
    self.bgView.height = orinY + 18*ADAPTER_WIDTH;
    self.height = self.bgView.bottom;
}
- (void)setParentVC:(BaseVC *)parentVC
{
    _parentVC = parentVC;
    [self.itemsArray enumerateObjectsUsingBlock:^(MLISThreeItemView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.parentVC = parentVC;
    }];
}
- (void)refreshUI
{
    CGFloat orinY = 39*ADAPTER_WIDTH;
    for (NSInteger index = 0; index < self.itemsArray.count; index ++) {
        MLISThreeItemView *view = self.itemsArray[index];
        view.top = orinY;
        orinY = view.bottom;
    }
    self.bgView.height = orinY + 18*ADAPTER_WIDTH;
    self.height = self.bgView.bottom;
    
    !self.callBackChangeHeightBlock ?: self.callBackChangeHeightBlock ();
}
#pragma mark - lazy懒加载
- (UIImageView *)topIV
{
    if (!_topIV) {
        UIImageView *iv = [[UIImageView alloc]init];
        [self addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"MLI_Segement_3"];
        iv.frame = CGRectMake((self.width - 273*ADAPTER_WIDTH) * 0.5, 0, 273*ADAPTER_WIDTH, 36*ADAPTER_WIDTH);
        _topIV = iv;
    }
    return _topIV;
}
- (UIView *)bgView
{
    if (!_bgView) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(14*ADAPTER_WIDTH, 8.2*ADAPTER_WIDTH, self.width - 28*ADAPTER_WIDTH, 0)];
        [self addSubview:view];
        view.layer.cornerRadius = 10*ADAPTER_WIDTH;
        //view.layer.masksToBounds = YES;
        view.layer.shadowColor = rgba(100, 100, 100, 1).CGColor;
        view.layer.shadowOffset = CGSizeMake(0, 0);
        view.layer.shadowRadius = 3;
        view.layer.shadowOpacity = 1;
        view.backgroundColor = WhiteColor;
        _bgView = view;
    }
    return _bgView;
}
- (NSMutableArray<MLISThreeItemView *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
@end
